#!/bin/bash
# @Author: leiwang
# @Date:   2018-09-30 10:03:00
# @Last Modified by:   leiwang
# @Last Modified time: 2018-11-02 15:31:18
# 构造monitor所在绝对路径，通过绝对路径启动monitor

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/sbin:/bin:${PATH}
# 可执行文件名配置
agent="dbAgent"
monitor="dbMonitor"

checkUser(){
  for i in `groups `; do
    if [ $i == "root" ]; then
      return 1
    fi
  done
  return 0
}
checkUser
ret=`echo $?`
if [ $ret != 1 ];then
    echo "WARN: User is not root!"
fi

# 动态获取脚本文件所在绝对路径
RUN_PATH=`cd $(dirname $0); pwd -P`


# 检查是否安装，保证安装后才能启动

# 找到支持开机重启的文件，用${autorunfile}表示
# centos /etc/rc.d/rc.local，若/etc/rc.local不是对前者的软链接则不会执行
# ubuntu, debian /etc/init.d/rc.local，若存在/etc/rc.local也会被执行
# suse /etc/rc.d/after.local
#
if [[ `cat /proc/version | grep coreos | wc -l` -gt 0 ]]; then
    # 假自启文件
    autorunfile="/etc/rc.local"
    if [ ! -e /etc/rc.local ]; then
        touch /etc/rc.local
        echo '#!/bin/bash -e' >> /etc/rc.local
        echo '# This file made by dbagent' >> /etc/rc.local
        chmod +x /etc/rc.local
    fi
elif [ ` lsb_release -a 2>/dev/null | grep -i "Ubuntu" | wc -l` -gt 0 ]; then
    autorunfile="/etc/rc.local"
    if [ ! -e /etc/rc.local ]; then
        touch /etc/rc.local
        echo '#!/bin/bash -e' >> /etc/rc.local
        echo '# This file made by dbagent' >> /etc/rc.local
        chmod +x /etc/rc.local
    fi
elif [ -e /etc/rc.d/rc.local ]; then
    # centos
    autorunfile="/etc/rc.d/rc.local"
elif [ -e /etc/rc.local ]; then
    # ubuntu, debian
    autorunfile="/etc/rc.local"
elif [ "`uname -a | grep -i debian | wc -l | tr -d ' '`"  -gt "0" ]; then
    # 9.X的Debian
    touch /etc/rc.local
    echo '#!/bin/sh -e' >> /etc/rc.local
    echo '# This file made by dbagent' >> /etc/rc.local
    chmod +x /etc/rc.local
    systemctl start rc-local
    autorunfile="/etc/rc.local"
else
    # suse, 可能有部分ubuntu也存在这个分支里目前 
    if [ ! -e /etc/rc.d/after.local ]; then
        touch /etc/rc.d/after.local
        echo '#!/bin/sh -e' >> /etc/rc.d/after.local
        echo '# This file made by dbagent' >> /etc/rc.d/after.local
    fi
    autorunfile="/etc/rc.d/after.local"
fi

# 保证安装过后才能启动agent
if [ "`grep ${RUN_PATH}/${monitor} ${autorunfile} | wc -l | tr -d ' '`"  -eq "0" ]; then
    echo "Warn: Agent does not support self-boot."
fi

# 检查重复启动
if [ "`ps -ef |grep ${RUN_PATH}/${monitor}|grep -v grep |wc -l|tr -d ' '`" -gt "0" ];then
    echo "Error: The dbagent has already be running now, please do not start again!"
    exit 7
fi

# 启动dbagent前检查配置文件，注意依赖配置文件名称agent.ini
# function __readINI() {
#     INIFILE=$1; SECTION=$2; ITEM=$3
#     _readIni=`awk -F '=' '/\['$SECTION'\]/{a=1}a==1&&$1~/'^$ITEM'/{print $2;exit}' $INIFILE`
#     echo ${_readIni}
# }
# serverip=$( echo $( __readINI ${RUN_PATH}/${agent}.ini AHA serverip ) | tr -d ' ' )
# if [[ ! ${serverip}  ]]; then
#     echo "Start dbagent failed. Please ensure that every items in configuration file have been configured such as serverip."
#     exit 
# fi
# buffersize=$( echo $( __readINI ${RUN_PATH}/${agent}.ini AHA buffersize ) | tr -d ' ' )
# if [[ $buffersize -lt '200' ]]; then
#     echo "Start dbagent failed for the buffersize in agent.ini is less than 200."
#     exit 
# elif [[ $buffersize -gt '600' ]]; then
#     echo "Start dbagent failed for the buffersize in agent.ini is greater than 600."
#     exit 
# fi


# 启动dbagent
echo "Start dbagent..."
nohup ${RUN_PATH}/${monitor} 1>/dev/null 2>/dev/null & 

# 循环探测启动是否成功，最大10s
for (( i = 0; i < 10; i++ ));do
    sleep 1s
if [ "`ps -ef |grep ${RUN_PATH}/${monitor}|grep -v grep |wc -l|tr -d ' '`" == "1" ];then
    break
fi
done

# 检查启动结果
if [ "`ps -ef |grep ${RUN_PATH}/${monitor}|grep -v grep |wc -l|tr -d ' '`" != "1" ];then
    echo "Start dbagent failed"
else
    echo "Start dbagent success"
fi
